import encrypt from '@/utils/encrypt.js'

const baseHeaders = {
    'app-name': 'web-pc',
    'env-honey': 'dev',
    'access-token': encrypt.GetLocalData('token') || '',
    version: '1.0.0'
}

const baseApiUrl = import.meta.env.VITE_BASE_URL

const makeRequest = async (endpoint, params = null, method = 'GET') => {
    const baseUrl = baseApiUrl.startsWith('/') ? window.location.origin : baseApiUrl
    const url = new URL(baseApiUrl.startsWith('/') ? `${baseUrl}${baseApiUrl}${endpoint}` : `${baseApiUrl}${endpoint}`)

    if (params && method === 'GET') {
        url.search = new URLSearchParams(params).toString()
    }

    const options = {
        method,
        headers: {
            ...baseHeaders,
            ...(method === 'POST' && { 'Content-Type': 'application/json' })
        }
    }

    if (params && method === 'POST') {
        const cdata = {
            header: {
                ...baseHeaders,
                'env-honey': ''
            },
            param: params
        }
        options.body = JSON.stringify({ data: encrypt.Encrypt(cdata) })
    }

    const response = await fetch(url, options)
    if (!response.ok) {
        throw new Error('Network response was not ok')
    }

    const data = await response.json()
    return import.meta.env.VITE_BASE_ENCRIPTION === 'false' ? data : encrypt.Decrypt(data)
}

// Upload image
export const uploadImgApi = async data => {
    const baseUrl = baseApiUrl.startsWith('/') ? window.location.origin : baseApiUrl
    const url = baseApiUrl.startsWith('/') ? `${baseUrl}${baseApiUrl}${import.meta.env.VITE_APP_NAME}/system/upload` : `${baseApiUrl}${import.meta.env.VITE_APP_NAME}/system/upload`

    const response = await fetch(url, {
        method: 'POST',
        headers: baseHeaders,
        body: data
    })
    if (!response.ok) {
        throw new Error('Network response was not ok')
    }
    return response.json()
}

// Get single unlocked card details
export const getCardDetailApi = params => {
    return makeRequest('/web/bot/card/detail', params, import.meta.env.VITE_BASE_ENCRIPTION === 'false' ? 'GET' : 'POST')
}

// Get bot cards list
export const getBotCardsApi = params => {
    return makeRequest('/web/bot/cards_v2', params, import.meta.env.VITE_BASE_ENCRIPTION === 'false' ? 'GET' : 'POST')
}

// Get chat image
export const getChatImageApi = params => {
    return makeRequest('/web/bot/get_chat_image_v2', params, import.meta.env.VITE_BASE_ENCRIPTION === 'false' ? 'GET' : 'POST')
}

// Get latest chat
export const getBotLastChatApi = params => {
    return makeRequest('/web/bot/last_chat_v2', params, import.meta.env.VITE_BASE_ENCRIPTION === 'false' ? 'GET' : 'POST')
}

// Get bot list
export const getBotListApi = params => {
    return makeRequest('/web/bot/get_class_bot_list', params, import.meta.env.VITE_BASE_ENCRIPTION === 'false' ? 'GET' : 'POST')
}

// Get bot class list
export const getBotClassListApi = params => {
    return makeRequest('/web/bot/role_class', params, import.meta.env.VITE_BASE_ENCRIPTION === 'false' ? 'GET' : 'POST')
}
